<template>
  <div style="margin: 10px 10px">
    <div style="margin-bottom: 5px">
      <el-input v-model="searchStuName" style="width: 150px;margin-right: 5px"></el-input>
      <el-button type="primary" @click="searchStuPer">搜索</el-button>
    </div>
    <div>
      <el-table :data="stuPers" border>
        <el-table-column
          prop="roleName"
          label="学生姓名"
          align="center"
          width="300">
        </el-table-column>
        <el-table-column
          prop="roleUsername"
          label="学生电话"
          align="center"
          width="420">
        </el-table-column>
        <el-table-column
          label="权限"
          align="center"
          prop="permissionName"
          width="300">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="200">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="showEdit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form
        :model="stuPermiss"
        label-width="80px">
        <el-form-item label="学生姓名">
          {{stuPermiss.roleName}}
        </el-form-item>
        <el-form-item label="学生电话">
          {{stuPermiss.roleUsername}}
        </el-form-item>
        <el-form-item label="权限">
          <el-select v-model="permissionId" placeholder="请选择">
            <el-option
              v-for="item in permissions"
              :key="item.permissionId"
              :label="item.permissionName"
              :value="item.permissionId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="update">确 定</el-button>
        </span>
    </el-dialog>
    <el-pagination
      background
      @current-change="currentChange"
      :current-page="currentPage"
      layout="prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import {editRecStuPer,initRecStuPer,getByName,getPermission} from '../../api/admin/recStu'

export default {
  data() {
    return {
      currentPage: 1,
      total: 100,
      permissionId: 0,
      dialogVisible: false,
      searchStuName: '',
      stuPers: [{
        roleId: 0,
        roleName: '',
        roleUsername: '',
        permissionId: 0,
        permissionName: ''
      }],
      permissions: [{
        permissionId: 0,
        permissionName: ''
      }],
      stuPermiss: {
        roleId: 0,
        roleName: '',
        roleUsername: '',
        permissionId: 0,
        permissionName: ''
      }
    }
  },
  methods: {
    // 查询当前页面数据
    currentChange(page){
      this.currentPage = page;
      initRecStuPer(this.currentPage).then(response=>{
        console.log(response)
        this.stuPers = response.data.records
        this.total = response.data.total
      })
    },
    // 通过姓名搜索
    searchStuPer() {
      if (this.searchStuName === ''){
        this.currentChange(this.currentPage)
      }else {
        getByName(this.searchStuName).then(response=>{
          console.log(response)
          this.stuPers = response.data.records
          this.total = response.data.total
        })
      }
    },
    // 展开编辑
    showEdit(row) {
      let old = JSON.stringify(row)
      this.stuPermiss = JSON.parse(old)
      this.permissionId = this.stuPermiss.permissionName
      this.dialogVisible = true
    },
    // 编辑
    update() {
      this.stuPermiss.permissionId =this.permissionId
      editRecStuPer(this.stuPermiss).then(response=>{
        this.$message({
          message: response.msg,
          type: 'success'
        })
        this.currentChange(this.currentPage)
        this.dialogVisible = false;
      })
    },
    // 获取权限列表
    getPer(){
      getPermission().then(response=>{
        this.permissions = response.data
      })
    }
  },
  created() {
    this.currentChange(1)
    this.getPer()
  }
}
</script>

<style scoped>

</style>
